﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Collections;

using mns.logdataDataSetTableAdapters;

namespace mns
{
    class Task
    {

        DateTime LastTime;      //
        int intervals;          //unit: minute
        int Delaytime;            //unit: minute
        public String name { get; set; }
        public int id { get; set; }
        public String htmlFilePath;
        public String TaskFilePath;
        
        public void Initial(String str)
        {
            StreamReader sr = File.OpenText(str);

            TaskFilePath = str;
            LastTime = DateTime.Parse(sr.ReadLine());
            intervals = Convert.ToInt32(sr.ReadLine());
            Delaytime = Convert.ToInt32(sr.ReadLine());
            sr.Close();
            
        }

        public bool Check()
        {
            //consider if the delaytime is equal to zero or not
            if(Delaytime == 0)
            {
                return LastTime.AddMinutes(intervals).CompareTo(DateTime.Now) > 0 ? false : true;
            }
            else
            {
                return LastTime.AddMinutes(Delaytime).CompareTo(DateTime.Now) > 0 ? false : true;
            }
            
        }

        public void Done()
        {
            Delaytime = 0;
            LastTime = DateTime.Now;
            string []str = {DateTime.Now.ToString(),intervals.ToString(),Delaytime.ToString()};
            File.WriteAllLines(TaskFilePath, str);  
        }

        public void Delay(int time)
        {   
            Delaytime = (int)(DateTime.Now - LastTime).TotalMinutes;
            Delaytime += time;
            string[] str = { DateTime.Now.ToString(), intervals.ToString(), Delaytime.ToString() };
            File.WriteAllLines(TaskFilePath, str); 
        }
        

    }

    class TaskManage
    {
        static String BaseDirectory;
        static String MachineKind;
        static Hashtable TaskList;

        private TaskManage()
        {
            ;
        }
        public static Task GetTask(int id)
        {
          
            if(TaskList.ContainsKey(id))
            {
                return (Task)TaskList[id];
            }

            return null;
        }
        
        static String CheckMachineKind()
        {
            return "\\FX";
        }

        static bool IsFolderRight(String name)
        {
            String work_name = name.Remove(0, (BaseDirectory + MachineKind).Length+1);
            if (work_name.Remove(5)=="task_")
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        static Task BuildTask(String name)
        {
            String work_filepath = name + "\\info.txt";
            if(File.Exists(work_filepath))
            {
                Task task = new Task();
                task.Initial(work_filepath);
                task.htmlFilePath = name + "\\task.html";
                task.name = name.Remove(0, (BaseDirectory + MachineKind).Length + 1);
                return task;

            }
            else
            {
                return null;
            }
    
        }

        public static void PeriodRun(ref List<int> processlist)
        {
            ICollection Tasks = TaskList.Values;

            foreach(Task task in Tasks)
            {
                
                if(task.Check())
                {
                   processlist.Add(task.id);
                }
            }

        }

        static void LoadMachineTasks()
        {
            String Work_Directory = BaseDirectory + MachineKind;
            List<String> dirs = new List<String>(Directory.EnumerateDirectories(Work_Directory));
            int i = 0;

            foreach (String dir in dirs)
            {
                if (IsFolderRight(dir))
                {
                    Task task = BuildTask(dir);
                    if(task!=null)
                    {
                        task.id = i++;
                        TaskList.Add(task.id, task);
                    }
                }
            }
            
        }

        public static void Initial()
        {

            TaskList = new Hashtable();
            BaseDirectory = Directory.GetCurrentDirectory() + "\\MNS";
            //query the type of machine     
            MachineKind = CheckMachineKind();
            //Load the tasks of this machine
            LoadMachineTasks();
        }

    }
}
